package com.jianggy.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *stream flatMap
 *@author jianggy
 *@date 2021/9/13 
 *@version 1.0
 **/
public class FlatMapTest {
    public static void main(String[] args) {
        String[] hello = new String[]{"hello", "world", "tomcat"};
        array2CharList(hello);
        String[] first = new String[]{"a", "b", "c", "d"};
        String[] second = new String[]{"1", "2", "3", "4", "5", "6"};
        combine2Array(first, second);
    }

    /**
     * 将字符串数组拆分为单个字符输出,例如输入["abc","def"],输出a,b,c,d,e,f
     * @param words 字符串数组
     * @return java.util.List<java.lang.String>
     * @author jianggy
     */
    public static List<String> array2CharList(String[] words) {
        Stream<String> stringStream = Arrays.stream(words);
        //lambda表达式方式
        List<String> collect = stringStream.flatMap(s -> Arrays.stream(s.split(""))).collect(Collectors.toList());
        for (String s : collect) {
            System.out.println(s);
        }
        return collect;
    }

    /**
     * 将两个数组中的元素进行组合。例如{"a", "b", "c", "d"}，{"1", "2", "3", "4", "5", "6"}，生成多个新的数组[a, 1][a, 2][a, 3][a, 4][a, 5][a, 6][b, 1][b, 2][b, 3][b, 4][b, 5][b, 6][c, 1][c, 2][c, 3][c, 4][c, 5][c, 6][d, 1] [d, 2][d, 3][d, 4][d, 5][d, 6]
     * @param first 第一个数组
     * @param second 第二个数组
     * @return java.util.List<java.lang.String [ ]>
     * @author jianggy
     */
    public static List<String[]> combine2Array(String[] first, String[] second) {
        Stream<String> firstStream = Arrays.stream(first);
        List<String[]> resList = firstStream.flatMap(s -> Arrays.stream(second).map(t -> new String[]{s, t})).collect(
                Collectors.toList());
        for (String[] strings : resList) {
            System.out.println(Arrays.toString(strings));
        }
        return resList;
    }
}
